package com.cskaoyan.shopping.api;

import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemServiceResponse;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailServiceResponse;
import com.cskaoyan.shopping.service.IProductService;
import com.cskaoyan.shopping.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * @auther cskaoyan
 * @date 2022/8/24:14:56
 */
@RestController
public class OrderApiImpl {

    @Autowired
    IProductService productService;
    @Autowired
    ShoppingCartService cartService;


    @GetMapping(value = "/rpc/product/detail")
    ServiceCallResponse<ProductDetailDto> getProductDetail(@RequestParam("id") Long id) {
        ProductDetailDto productDetail = productService.getProductDetail(id);
        ServiceCallResponseUtil<ProductDetailDto> responseUtil = new ServiceCallResponseUtil<>();
        return responseUtil.ok(productDetail);
    }

    /**
     * @Author: 宾文杰 @Description: 对应order服务的调用 @Date: 2022/9/21
     */
    @DeleteMapping("rpc/cart/items")
    public ClearCartItemServiceResponse deleteItemsFromOrder(ClearCartItemRequest request) {
        @NotNull Long userId = request.getUserId();
        cartService.deleteItems(userId);
        return (ClearCartItemServiceResponse) new ServiceCallResponseUtil<>().ok(null);
    }
}
